<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.global.js"></script>
  <script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>
  <script>
    const { createApp } = Vue;
    const emitter = mitt();
   
    const app = createApp();
    const CommonChild = {
      template: `
        <div>
          <h3>这是子组件1</h3>  
          <button @click="biubiu">按钮</button>
        </div>
      `,
      data() {
        return {
          msg: '这是子组件1数据'
        }
      },
      methods: {
        biubiu(){
          emitter.emit('piupiu', this.msg);
        }
      }
    }
    const CommonChild2 = {
      template: `
        <div>
          <h3>这是子组件2</h3>  
          {{ msg }}
        </div>
      `,
      data() {
        return {
          msg: ''
        }
      },
      created(){
        /* emitter.on('piupiu', (msg) => {
          console.log('兄弟我收到了', msg);
          this.msg = msg;
        }) */
        emitter.on('piupiu', function(msg){
          console.log('兄弟我收到了', msg);
          this.msg = msg;
        }.bind(this))
      }
    }
    const CommonHead = {
      template: `
        <div>
          <h2>父组件</h2>  
          <hr/>
          <common-child></common-child>
          <common-child2></common-child2>
        </div>
      `,
      components: {
        CommonChild,
        CommonChild2
      }
    }
    app.component('CommonHead', CommonHead);
    app.mount('#app')
  </script>
</body>
</html>